﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace VuzixSample.model.snes
{
    class Utilities
    {
        //http://romhack.wikia.com/wiki/SNES_ROM_layout#Formulas_to_convert_addresses

        public static uint fileToSnesAddress(uint addr, bool isHeader, SnesRom.MapperTypes mapper)
        {
            if (isHeader) addr -= 512;

            switch (mapper)
            {
                case SnesRom.MapperTypes.LoROM:
                    {
                        addr = ((addr & 0x7f8000) << 1) + 0x8000 + (addr & 0x7fff);
                    } break;

                case SnesRom.MapperTypes.HiROM:
                    {
                        addr = 0xc00000 + (addr & 0x3fffff);
                    } break;
            }

            return addr;
        }
        public static uint SnesToFileAddress(uint addr, bool isHeader, SnesRom.MapperTypes mapper)
        {
            switch (mapper)
            {
                case SnesRom.MapperTypes.LoROM:
                    {
                        addr = ((addr & 0x7f0000) >> 1) + (addr & 0x7fff);
                    } break;

                case SnesRom.MapperTypes.HiROM:
                    {
                        addr = addr & 0x3fffff;
                    } break;
            }

            if (isHeader) addr += 512;

            return addr;
        }
    }
}
